<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 100%;overflow-y: hidden">
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        取消
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item  label="工程名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入工程名称" v-decorator.trim="[ 'engineerName' ]"  :readOnly="true" />
        </a-form-item>
        <a-form-item label="补充内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea :rows="4" placeholder="请输入补充内容" v-decorator.trim="[ 'description', validatorRules.description]"  />
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>
<script>
  import pick from 'lodash.pick'
  import Vue from 'vue'
  import JSelectPosition from '@/components/jeecgbiz/JSelectPosition'
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  import {addEngineerSupply} from '@/api/api'
  export default {
    name: "EngineerSupplyModal",
    components: {
      JSelectPosition
    },
    data () {
      return {
        title:"操作",
        visible: false,
        modalWidth:800,
        drawerWidth:700,
        id:"", 
        engineerId:"",
        isReadOnly: false,
        disableSubmit:false,
        dateFormat:"YYYY-MM-DD",
        validatorRules:{
          description:{
            rules: [{
              required: true, message: '请输入补充内容!'
            }]
          }
        },
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        uploadLoading:false,
        confirmLoading: false,
        headers:{},
        form:this.$form.createForm(this)
      }
    },
    created () {
      const token = Vue.ls.get(ACCESS_TOKEN);
      this.headers = {"X-Access-Token":token}
    },
    methods: {
       add (record) {
        this.edit(record);
      },
      edit (record) {
        this.form.resetFields();
        this.visible = true;
        this.model = Object.assign({}, record);
        this.model.engineerName = record.name;
        this.engineerId = record.id;
        this.model.id = "";
        this.model.description = "";
        this.editAfter(this.model);
      },
            //调用完edit()方法之后会自动调用此方法
      editAfter() {
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.model,'engineerName','description'))
          });
      },
      close() {
        this.$emit('close');
        this.visible = false;
        this.disableSubmit = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            formData.engineerId = this.engineerId;
            let obj;
            obj=addEngineerSupply(formData);
            obj.then((res)=>{
              if(res.code === 200){
                that.$emit('ok');
              }else{
                that.$message.warning(res.data.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })

          }
        })
      },
      handleCancel() {
        this.close()
      }
    }
  }
</script>

<style scoped>

</style>